package com.xrz.controller;

import com.xrz.common.Result;
import com.xrz.dao.GroupDao;
import com.xrz.dao.GroupInfoDao;
import com.xrz.dao.QaqUserDao;
import com.xrz.pojo.Group;
import com.xrz.pojo.GroupInfo;
import com.xrz.pojo.vo.GroupInfoVo;
import com.xrz.pojo.vo.GroupVo;
import com.xrz.pojo.vo.QaqUserVo;
import com.xrz.service.GroupService;
import com.xrz.utils.FileUpload;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "群controller", tags = {"群聊操作接口"})
@RestController
@RequestMapping("/group")
public class GroupController {

    @Resource
    GroupInfoDao groupInfoDao;
    @Resource
    QaqUserDao qaqUserDao;
    @Resource
    GroupDao groupDao;
    @Autowired
    GroupService groupService;

    //获取全部群信息
    @ApiOperation(value = "获取所有群")
    @GetMapping("/getAllGroup")
    public Result<?> getAllGroup() {
        List<Group> groups = groupDao.getAllGroup();
        return Result.success(groups);
    }
    //获取我加入的群
    @ApiOperation(value = "获取我加入的群")
    @GetMapping("/getMyGroup/{userId}")
    public Result<?> getMyGroup(@PathVariable Integer userId) {
        List<Group> groups = groupDao.getMyGroup(userId);
        return Result.success(groups);
    }

    //加入群
    @ApiOperation(value = "加入群")
    @PostMapping("/joinGroup/{groupId}/{userId}")
    public Result<?> joinGroup(@PathVariable Integer userId, @PathVariable Integer groupId) {
        List<GroupInfo> groups=groupInfoDao.selectByGroupId(groupId);
        for (GroupInfo g:groups){
            if(g.getGroupUserId().equals(userId)){
                return Result.error(0,"您已在群里");
            }
        }
        GroupInfo groupInfo=new GroupInfo();
        groupInfo.setGroupId(groupId);
        groupInfo.setGroupUserId(userId);
        groupInfoDao.insertSelective(groupInfo);
        return Result.success("加入成功");
    }

    //退出群
    @ApiOperation(value = "退出群")
    @PostMapping("/exitGroup/{groupId}/{userId}")
    public Result<?> exitGroup(@PathVariable Integer userId, @PathVariable Integer groupId) {

        return groupService.exitGroup(userId,groupId);
    }

    //获取群人数信息
    @ApiOperation(value = "获取群人数")
    @GetMapping("/getGroupCount/{groupId}")
    public Result<?> getGroupCount(@PathVariable Integer groupId) {
        int a = groupInfoDao.getGroupCountByGroupId(groupId);
        return Result.success(a);
    }
    //模糊搜索群名
    //群信息，人数
    @ApiOperation(value = "模糊搜索群名")
    @GetMapping("/getGroupInfo/{groupName}")
    public Result<?> getGroupInfo(@PathVariable String groupName) {
        List<GroupVo> groups=groupDao.getGroup(groupName);

        return Result.success(groups);
    }


    //获取群信息和群成员信息
    @ApiOperation(value = "获取群信息和群成员")
    @GetMapping("/getGroupMember/{groupId}")
    public Result<?> getGroupMember(@PathVariable Integer groupId) {
        Group group=groupDao.selectByPrimaryKey(groupId);
        List<GroupInfoVo> groupMember = groupInfoDao.selectGroupUserId(groupId);
        List<QaqUserVo> qaqUsers = qaqUserDao.selectByPrimaryKeys(groupMember);
        Map map=new HashMap();
        map.put("group",group);
        map.put("qaqUsers",qaqUsers);
        map.put("count",qaqUsers.size());
        return Result.success(map);
    }

    //创建群
    @ApiOperation(value = "创建群")
    @PostMapping(value = "/newGroup/{userId}",produces = "application/json;charset=utf-8")
    public Result<?> newGroup(Group group, @RequestParam(value = "avatar") MultipartFile file,@PathVariable Integer userId) throws IOException {

        group.setGroupImg(FileUpload.saveFile(file));

        Group group1=groupDao.selectByGroupName(group.getGroupName());
        if (group1!=null){
            return Result.error(0, "群名称不可用");
        }
        int res = groupDao.insertSelective(group);//创建群聊
        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setGroupId(group.getGroupId());
        groupInfo.setGroupUserId(userId);
        int res1 = groupInfoDao.insert(groupInfo);//修改群信息
        if (res == 1&& res1==1) {
            return Result.success("创建成功");
        }
        return Result.error(0, "创建失败");
    }


    @PostMapping("imgUp")
    public Result<?> imgUp(@RequestParam(value = "avatar") MultipartFile file) throws IOException {
        return  Result.success(FileUpload.saveFile(file));
    }



    //解散群
    @ApiOperation(value = "解散群")
    @PostMapping("/killGroup/{groupId}")
    public Result<?> killGroup(@PathVariable Integer groupId) {
        int res = groupDao.deleteByPrimaryKey(groupId);
        int res1 = groupInfoDao.deleteByGroupId(groupId);
        if (res == 1 && res1 == 1) {
            return Result.success("解散成功");
        }
        return Result.error(0, "解散失败");
    }
}
